package com.xjtu.duansheli.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjtu.duansheli.entity.ProductCommentsInfo;
import com.xjtu.duansheli.entity.UserInfo;
import com.xjtu.duansheli.exception.MyException;
import com.xjtu.duansheli.mapper.ProductCommentsInfoMapper;
import com.xjtu.duansheli.service.ProductCommentsInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 漩涡鸣人
 * @since 2020-10-24
 */
@Service
public class ProductCommentsInfoServiceImpl extends ServiceImpl<ProductCommentsInfoMapper, ProductCommentsInfo> implements ProductCommentsInfoService {

    @Autowired
    private ProductCommentsInfoMapper productCommentsInfoMapper;

    @Override
    public List<ProductCommentsInfo> getAllProductCom(int current, int size) {
        Page<ProductCommentsInfo> page = new Page<>(current,size);
        productCommentsInfoMapper.selectPage(page, null);
        System.out.println(page.getTotal());
        return page.getRecords();
    }

    @Override
    public ProductCommentsInfo getProductComByid(Long cid) {
        ProductCommentsInfo productCommentsInfo = productCommentsInfoMapper.selectById(cid);
        if(productCommentsInfo == null){
            throw new MyException(500,"不存在该评论！");
        }
        return productCommentsInfo;
    }

    @Override
    public int addProductCom(ProductCommentsInfo productCom) {
        try{
            return productCommentsInfoMapper.insert(productCom);
        }catch (Exception e){
            throw new MyException(500,"添加评论失败！");
        }
    }

    @Override
    public int updateProductCom(ProductCommentsInfo productCom) {
        try{
            return productCommentsInfoMapper.updateById(productCom);
        }catch (Exception e){
            throw new MyException(500,"修改评论失败！");
        }
    }

    @Override
    public int deleteProductCom(Long pid) {
        try{
            return productCommentsInfoMapper.deleteById(pid);
        }catch (Exception e){
            throw new MyException(500,"删除评论失败！");
        }
    }

    @Override
    public List<ProductCommentsInfo> getProductComBypid(Long pid) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("product_id",pid);
        List<ProductCommentsInfo> productCommentsInfos = productCommentsInfoMapper.selectByMap(map);
        if(productCommentsInfos.size() == 0){
            throw new MyException(500,"该商品目前无评论！");
        }
        return productCommentsInfos;
    }

    @Override
    public List<ProductCommentsInfo> getProductComByaid(Long aid) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("author_id",aid);
        List<ProductCommentsInfo> productCommentsInfos = productCommentsInfoMapper.selectByMap(map);
        if(productCommentsInfos.size() == 0){
            throw new MyException(500,"该用户目前未发表任何评论！");
        }
        return productCommentsInfos;
    }

    @Override
    public List<ProductCommentsInfo> getProductComByparentId(Long paid) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("parent_id",paid);
        List<ProductCommentsInfo> productCommentsInfos = productCommentsInfoMapper.selectByMap(map);
        if(productCommentsInfos.size() == 0){
            throw new MyException(500,"该评论目前无子评论！");
        }
        return productCommentsInfos;
    }

    @Override
    public int getTotal() {
        return productCommentsInfoMapper.selectCount(null);
    }
}
